﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using EbbinghausWord.BLL;
using EbbinghausWord.Common;
using EbbinghausWord.DAL;
using EbbinghausWord.Entities;
using Microsoft.Phone.Controls;

namespace EbbinghausWord.Pages
{
    public partial class SelectBookPage : PhoneApplicationPage
    {
        List<BookEntity> books = null;

        public SelectBookPage()
        {
            InitializeComponent();

            loadingControl.Loading(() =>
                                       {
                                           var et = new ExecuteTimer(false);
                                           books = BookDAL.GetAllBookDetails();
                                           et.Show("GetAllBookDetails");
                                       },
                () =>
                {
                    selectBookListBox.ItemsSource = books;
                });


        }

        private int _originBookId = new UserConfig().UserBookId;

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            var bookEntity = ((sender as FrameworkElement).Tag as BookEntity);
            var electBookId = bookEntity.BookId;
            
            if (MessageBox.Show(string.Format("您选择了 {0}，是否进行学习？", bookEntity.BookName),
                                "选择课本", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel) return;

            var et = new ExecuteTimer(false);

            //设置当前用户的书籍Id
            new UserConfig().UserBookId = electBookId;

            //单词页面数据需要刷新
            WordBookManager.IsNeedReflesh = true;

            //初始化当前单词本
            BookDAL.InitCurrentBookWord();

            //初始化用户单词本
            WordBookManager.InitUserWord();
            et.Show("InitUserWord");

            //刷新界面
            selectBookListBox.ItemsSource = null;
            selectBookListBox.ItemsSource = books;

            //返回到学习页面
            if (_originBookId == 0)
            {
                NavigationService.Navigate(new Uri(@"/Pages/ReviewWordPage.xaml", UriKind.RelativeOrAbsolute));
            }
        }
    }
}